from http.server import BaseHTTPRequestHandler, HTTPServer
import json
from urllib.parse import urlparse, parse_qs
import voice


class SimpleAPIHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        parsed_path = urlparse(self.path)
        query_params = parse_qs(parsed_path.query)
        #ok = voice.ok()
        if parsed_path.path == "/hello":
            self.send_response(200)
            self.send_header("Content-Type", "application/json")
            self.end_headers()
            response = {"message": f"Hello, World!"}
            self.wfile.write(json.dumps(response).encode("utf-8"))

        elif parsed_path.path == "/ok":
            name = query_params.get("name", ["World"])[0]
            self.send_response(200)
            self.send_header("Content-Type", "application/json")
            self.end_headers()
            response = {"message": f"ok!{name}"}
            self.wfile.write(json.dumps(response).encode("utf-8"))
        else:
            self.send_response(404)
            self.end_headers()
            self.wfile.write(b"404 Not Found")


if __name__ == "__main__":
    host = "localhost"
    port = 8000
    server = HTTPServer((host, port), SimpleAPIHandler)
    print(f"Server running on http://{host}:{port}")
    server.serve_forever()